﻿using cfg;
using FrameCore.LogSystem;
using FrameCore.TimeHelpSystem;
using Luban;
using Server.Module;
using System;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Server.Core
{
    class Launch
    {
        private static EServerStatu serverStatu = EServerStatu.None;
        private static long lastTime;
        private static long curTime;
        static void Main(string[] args)
        {
            Log.Info("服务器开始启动。。。");
            
            // 订阅应用程序关闭事件
            AppDomain.CurrentDomain.ProcessExit += OnProcessExit;

            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            GlobalParam.SelfPath = Path.GetDirectoryName(path);

            serverStatu = EServerStatu.InPreparation;

            InitConfig();

            Log.Info("服务器启动完成，开始运行逻辑。。。");

            lastTime = 0;
            curTime = 0;

            while (true) 
            {
                if (serverStatu == EServerStatu.Running)
                {
                    RunningLogic();
                }
            }
        }
        static void InitConfig()
        {
            Log.Info("配置文件获取中。。。");
            Tables.Self = new Tables(LoadByteBuf);
            GlobalParam.LogicTime = Tables.Self.AllSetParam.GetOrDefault("LogicFrame").IntValue;
            GlobalParam.HeartInteralTime = Tables.Self.AllSetParam.GetOrDefault("HeartInteralTime").IntValue;

            GlobalParam.ServerId = 1;

            ServerDataItem serverDataItem = Tables.Self.ServerData.GetOrDefault(GlobalParam.ServerId);
            GlobalParam.UseModuleId = serverDataItem.ModuleGroupId;

            Log.Info("服务器开始构建");
            //模块初始化
            ModuleManager.Init();
            Log.Info("服务器构建完成");
            ModuleManager.Start();
        }
        static void RunningLogic()
        {
            curTime = TimeHelp.GetNowTimeMS();

            //运行帧
            if (curTime - lastTime >= GlobalParam.LogicTime)
            {
                GlobalParam.CurLogicTime += GlobalParam.LogicTime;
                lastTime = curTime;
                EarlyUpdate();
                Update();
                LateUpdate();
            }
        }
        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        static ByteBuf LoadByteBuf(string file)
        {
            string gameConfDir = $"{GlobalParam.SelfPath}/DataTable/DataConfig/{file}.bytes";
            return new ByteBuf(File.ReadAllBytes(gameConfDir));
        }

        static void EarlyUpdate()
        {
            ModuleManager.EarlyUpdate();
        }
        static void Update()
        {
            ModuleManager.Update();
        }
        static void LateUpdate()
        {
            ModuleManager.LateUpdate();
        }
        static void OnProcessExit(object sender, EventArgs e)
        {
            serverStatu = EServerStatu.Close;
            ModuleManager.Realease();
        }
    }
}
